/**
 * 
 */
$(function(){
	$(".form_body_head").change(function() {	//注册类型单选按钮发生变化时触发的函数
		var head_value = $('.form_body_head input[name = "user_type"]:checked').val();
		if(head_value == 1) {
			$("#user_form").css("display", "block");
			$("#business_form").css("display","none");
		}
		else{
			$("#user_form").css("display","none");
			$("#business_form").css("display","block");
		}
	})
	
	$("#user_register_btn, #business_register_btn").click(function () {	//点击注册时触发的函数
		var reg_value = {};					//正则表达式数组
		reg_value["user_name"] =/^([\u4e00-\u9fa5]+|([a-z]+\s?)+)$/;
		reg_value["user_email"] = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ ;
		reg_value["user_id"] = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
		reg_value["business_id"]  = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
		reg_value["business_email"] = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ ;
		var eles,ele_val,err=0;
		var head_value = $('.form_body_head input[name = "user_type"]:checked').val();
		if(head_value == 1) {
			eles = $("#user_form").find("input,textarea");
			eles.each(function () {
				ele_val = $(this).val();
				var  number = $(this).attr("tabindex");
				if(number == 1) {		//姓名输入框规则
					if(ele_val == null || ele_val == "") {
						$("#user_name + span").html("姓名不能为空");
						err++;
					}
					else if(!reg_value["user_name"].test(ele_val)) {
						$("#user_name + span").html("姓名格式不正确");
						err++;
					}
					else {
						
						$("#user_name + span").html("√");
					}
				}
				else if(number == 2) {	//密码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#user_password + span").html("密码不能为空");
						err++;
					}
					else{
						$("#user_password + span").html("√");
					}
				}
				else if(number == 3) {		//确认密码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#user_password2 + span").html("确认密码不能为空");
						err++;
					}
					else if($("#user_password").val() != $("#user_password2").val()) {
						$("#user_password2 + span").html("两次密码不一致");
						err++;
					}
					else{
						$("#user_password2 + span").html("√");
					}
				}
				else if (number == 4) {		//邮箱地址输入框规则
					if(ele_val != null && ele_val != "") {
						if(!reg_value["user_email"].test(ele_val)) {
							$("#user_email + span").html("邮箱格式不正确");
							err++;
						}
						else{
							$("#user_email + span").html("√");
						}
					}
					else{
						$("#user_email + span").html("");
					}
					
				}
				else if(number == 5) {		//电话号码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#user_phone + span").html("电话号码不能为空");
						err++;
					}
					else if(!reg_value["user_id"].test(ele_val)) {
						$("#user_phone + span").html("电话号码格式不正确");
						err++;
					}
					else {
						$("#user_phone + span").html("√");
					}
				}
			})
			if(!err) {		//没有出现错误时的处理
			$.ajax({    // 将数据发送到服务器
	            url:"personal_users/register.action",  //等待接收新密码的文件地址
	            data: 
//	            { 
	            	"user.u_name="+ $("#user_name").val()+   //姓名
	            	"&user.u_password=" +$("#user_password").val()+//密码
	            	"&user.sex="+ $('#user_form input[name = "user_sex"]:checked').val()+//性别
	            	"&user.E_mail="+ $("#user_email").val()+  //邮箱
	            	"&user.phone="+ $("#user_phone").val()+ //电话号码
	            	"&user.education="+ $("#education option:selected").text()+ //学历
	            	"&user.experience="+ $("#user_work_experience").val()+ //工作经历
	            	"&user.self_introduction="+ $("#user_introduce").val(), // 自我介绍
            
//	            	},
//	            dataType:"json",
	            type:"post",
	            success:function(data) {
	            	if(data==true) {
	            		alert("注册成功！");
	           		 }
	            	else{
	            		alert("注册失败！");
	            		
	            	}
				},
				error:function(request) {
					alert("注册失败！");
				}
	        })
		}
		else{
			alert("填写信息错误！");
		}
		}
		else {
			eles = $("#business_form").find("input,textarea");
			eles.each(function () {
				ele_val = $(this).val();
				var  number = $(this).attr("tabindex");
				if(number == 1) {		//公司名称输入框规则
					if(ele_val == null || ele_val == "") {
						$("#business_name + span").html("公司名称不能为空");
						err++;
					}
					else{
						$("#business_name + span").html("√");
					}
				}
				else if(number == 2) {	//密码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#business_password + span").html("密码不能为空");
						err++;
					}
					else{
						$("#business_password + span").html("√");
					}
				}
				else if(number == 3) {		//确认密码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#business_password2 + span").html("确认密码不能为空");
						err++;
					}
					else if($("#business_password").val() != $("#business_password2").val()) {
						$("#business_password2 + span").html("两次密码不一致");
						err++;
					}
					else{
						$("#business_password2 + span").html("√");
					}
				}
				else if (number == 4) {		//邮箱地址输入框规则
					if(ele_val != null && ele_val != "") {
						if(!reg_value["business_email"].test(ele_val)) {
							$("#business_email + span").html("邮箱格式不正确");
							err++;
						}
						else{
							$("#business_email + span").html("√");
						}
					}
					else{
						$("#business_email + span").html("");
					}
					
				}
				else if(number == 5) {		//电话号码输入框规则
					if(ele_val == null || ele_val == "") {
						$("#business_phone + span").html("电话号码不能为空");
						err++;
					}
					else if(!reg_value["business_id"].test(ele_val)) {
						$("#business_phone + span").html("电话号码格式不正确");
						err++;
					}
					else{
						$("#business_phone + span").html("√");
					}
				}
				else if(number == 6) {		//许可证输入框规则
					if(ele_val == null || ele_val == "") {
						$("#business_licence + span").html("许可证不能为空");
						err++;
					}
					else{
						$("#business_licence + span").html("√");
					}
				}
			})
			if(!err) {		//没有出现错误时的处理
			$.ajax({    // 将数据发送到服务器
	            url:"merchant_users/register.action",  //等待接收新密码的文件地址
	            data: 
	            	
	            	"merchant_user.company_name="+ $("#business_name").val()+ //公司名称
	                "&merchant_user.m_password="+ $("#business_password").val()+ //密码
	            	"&merchant_user.E_mail="+$("#business_email").val()+ //邮箱
	            	"&merchant_user.phone="+ $("#business_phone").val()+ //电话号码
	            	"&merchant_user.permit_id="+ $("#business_licence").val()+ //公司许可证
	            	"&merchant_user.company_profile="+$("#business_introduce").val(), //公司简介
	            type:"post",
	            success:function(data) {
	            	if(data==true) {
	            		alert("注册成功！");
	            		
	           		 }
	            	else{
	            		alert("注册失败！");
	            		
	            	}
				},
				error:function(request) {
					alert("注册失败！");
				}
	        })
		}
		else{
			alert("填写信息错误！");
		}
		}
		
	})
	window.location.href="login.html";
})
